package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.*;
import java.util.concurrent.atomic.AtomicMarkableReference;

/***
 * title AdminTopicServiceImpl
 * @description <TODO description class purpose>
 * @author ZCZ
 * @version 1.0.0
 * @create 2022/9/11 12:06
 **/
@Service
public class AdminTopicServiceImpl implements AdminTopicService {

    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public CommonData<MarketTopic> list(BasePageInfo info, String title, String subtitle) {
        //开启分页
        PageHelper.startPage(info.getPage(),info.getLimit());
        //创建example，用于在数据库中查询数据，包含查询条件和排序条件
        MarketTopicExample example = new MarketTopicExample();
        example.setDistinct(true);
        example.setOrderByClause(info.getSort()+" "+info.getOrder());
        //拼接criteria的条件
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if(title!=null){
            criteria.andTitleLike("%"+title+"%");
        }
        if(subtitle!=null){
            criteria.andSubtitleLike("%"+subtitle+"%");
        }
        List<MarketTopic> marketTopicList = marketTopicMapper.selectByExample(example);
        //把list传进返回值data中
        CommonData<MarketTopic> marketTopicCommonData = new CommonData<>();

        //使用pagehelper把data分页
        PageInfo pageInfo = new PageInfo(marketTopicList);
        Integer total = (int)pageInfo.getTotal();
        int pages = pageInfo.getPages();
        marketTopicCommonData.setTotal(total);
        marketTopicCommonData.setPages(pages);
        marketTopicCommonData.setList(marketTopicList);
        return marketTopicCommonData;
    }

    @Override
    public MarketTopic create(MarketTopic marketTopic) {
        Date date = new Date();
        marketTopic.setAddTime(date);
        marketTopic.setUpdateTime(date);
        marketTopicMapper.insertSelective(marketTopic);
        MarketTopic marketTopic1 = marketTopicMapper.selectByPrimaryKey(marketTopic.getId());
        return marketTopic1;
    }

    @Override
    public void delete(Integer id) {
        // marketTopicMapper.deleteByPrimaryKeyLogic(id);
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        marketTopic.setDeleted(true);
        marketTopic.setUpdateTime(new Date());
        marketTopicMapper.updateByPrimaryKey(marketTopic);
    }

    @Override
    public MarketTopic update(MarketTopic marketTopic) {
        marketTopicMapper.updateByPrimaryKey(marketTopic);
        MarketTopic marketTopic1 = marketTopicMapper.selectByPrimaryKey(marketTopic.getId());
        marketTopic1.setUpdateTime(new Date());
        return marketTopic1;
    }

    @Override
    public MarketTopicGoods read(int id) {
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        // List list = new ArrayList(Arrays.asList(a));
        //用来判断该专题有没有商品
        List<MarketGoods> goodsList = null;
        if(marketTopic.getGoods().length>0){
            List list = new ArrayList(Arrays.asList(marketTopic.getGoods()));
            MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
            marketGoodsExample.setDistinct(true);
            MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
            criteria.andIdIn(list);
            goodsList = marketGoodsMapper.selectByExample(marketGoodsExample);
        }
        MarketTopicGoods marketTopicGoods = new MarketTopicGoods();
        marketTopicGoods.setGoodsList(goodsList);
        marketTopicGoods.setTopic(marketTopic);
        return marketTopicGoods;
    }

    @Override
    public void batchDelete(Map<String,Integer[]> map) {
        MarketTopicExample example = new MarketTopicExample();
        // MarketTopicExample.Criteria criteria = example.createCriteria();
        Integer[] ids = map.get("ids");
        // Integer[] value=null;
        // for (Map.Entry<String, Integer[]> stringEntry : map.entrySet()) {
        //     value = stringEntry.getValue();
        // }
        if (ids.length>0) {
            for (Integer id : ids) {
                MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
                marketTopic.setDeleted(true);
                marketTopic.setUpdateTime(new Date());
                marketTopicMapper.updateByPrimaryKey(marketTopic);
            }
        }
    }
}
